<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<html>
<body>
	<h2>Hello World!</h2>
	<shiro:guest>
		<p>welcome，guest！请<a href='<c:url value="/login"></c:url>'><font color="red">登录</font></a></p>
	</shiro:guest>
	<shiro:authenticated>
		<h3>welcome，<shiro:principal defaultValue="未登录用户"/></h3>
		<a href="<c:url value='/logoutxxxx'/>">登出</a>
		<hr>
	</shiro:authenticated>
	<fieldset>
		<legend>受保护的视图</legend>
		<a href='<c:url value="/login"></c:url>'>login.jsp</a><br>
		<shiro:hasRole name="user">
			<a href='<c:url value="/user/index"></c:url>'>user.jsp</a><br>
		</shiro:hasRole>
		<shiro:hasRole name="admin">
			<a href='<c:url value="/admin/index"></c:url>'>admin.jsp</a><br><br>
		</shiro:hasRole>
		<shiro:hasPermission name="user:query">
			<button id="queryUser">查询用户</button>&nbsp;&nbsp;<label for="id">ID:</label><input id="id" name="id"><br><br>
		</shiro:hasPermission>
		<shiro:hasRole name="admin">
			<button id="listUser">用户列表</button><br><br>
		</shiro:hasRole>
		<shiro:hasPermission name="user:add">
			<fieldset>
				<legend>添加用户</legend>
				<button id="addUser">添加用户</button><br>
				<label for="username">UserName:</label><input name="username" id="username"><br>
				<label for="password">Password:</label><input name="password" id="password"><br>
			</fieldset>
		</shiro:hasPermission>
	</fieldset>
	<fieldset>
		<legend>不受保护的视图</legend>
		<a href='<c:url value="/login"></c:url>'>login.jsp</a><br>
		<a href='<c:url value="/user/index"></c:url>'>user.jsp</a><br>
		<a href='<c:url value="/admin/index"></c:url>'>admin.jsp</a><br><br>
		<button id="queryUser">查询用户</button>&nbsp;&nbsp;<label for="id">ID:</label><input id="id" name="id"><br><br>
		<button id="listUser">用户列表</button><br><br>
		<fieldset>
			<legend>添加用户</legend>
			<button id="addUser">添加用户</button><br>
			<label for="username">UserName:</label><input name="username" id="username"><br>
			<label for="password">Password:</label><input name="password" id="password"><br>
		</fieldset>
	</fieldset>
	<fieldset>
		<legend>操作结果</legend>
		<textarea id="result" rows="20" cols="100" readonly="readonly"></textarea>
	</fieldset>
	
	
	<input type="hidden" id="ctx" value='<c:url value="/"></c:url>'>
    <script type="text/javascript" src="<c:url value="/static/js/jquery-1.11.0.min.js"/>"></script>
    <script type="text/javascript">
    	var ctx = $("#ctx").val();
    	$("#addUser").click(function(){
    		$.ajax({
    			url:ctx+"user/add",
    			type:"POST",
    			data:{
    				username:$("#username").val(),
    				password:$("#password").val()
    			},
    			dataType:"json",
    			success:function(result){
    				var data = result && result.data;
    				var msg = result && result.msg;
    				$("#result").val((data && JSON.stringify(data, null, 4)) || (msg||"程序异常"));
    			},
    			error:function (XMLHttpRequest, textStatus, errorThrown){
    				$("#result").val('无权限');
    			}
    			
    		});
    		return ;
    	});
    	$("#listUser").click(function(){
    		$.ajax({
    			url:ctx+"user/list",
    			dataType:"json",
    			success:function(result){
    				var data = result && result.data;
    				var msg = result && result.msg;
    				$("#result").val((data && JSON.stringify(data, null, 4)) || (msg||"程序异常"));
    			},
    			error:function (XMLHttpRequest, textStatus, errorThrown){
    				$("#result").val('无权限');
    			}    			
    		});
    	});
    	$("#queryUser").click(function(){
    		$.ajax({
    			url:ctx+"user/one",
    			data:{
    				id:$("#id").val()
    			},    			
    			dataType:"json",
    			success:function(result){
    				var data = result && result.data;
    				var msg = result && result.msg;
    				$("#result").val((data && JSON.stringify(data, null, 4)) || (msg||"程序异常"));
    			},
    			error:function (XMLHttpRequest, textStatus, errorThrown){
    				$("#result").val('无权限');
    			}    			
    		});
    	});
    </script>	
</body>
</html>
